﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.Album
{
    public partial class MyAlbum : System.Web.UI.Page
    {

        protected int currentpage = 1;//当前页
        protected int pagesize = 4;//页记录大小            
        protected List<int?> count;
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;//暂用用户ID，写死，到时候用COOKIE替换过来
            int countAlbum = 0;

            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            //把被查询页赋值给当前页
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                currentpage = Convert.ToInt32(Request.QueryString["page"]);
            }

            userID = int.Parse(strUserIdFromCookie);
            string strOtherID = Request.QueryString["userID"];
            if (!string.IsNullOrEmpty(strOtherID))
            {
                userID = Convert.ToInt32(strOtherID);
            }
            StringBuilder indexHtml = new StringBuilder(Cache["user_albummy"].ToString());
            indexHtml = indexHtml.Replace("$Header$", Cache["user_header2"].ToString());
            indexHtml = indexHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            indexHtml = indexHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            indexHtml = indexHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());

            Jy123.Model.User_Main modelUser = new Jy123.Model.User_Main();
            Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
            modelUser = bllUser.GetModel(userID);
            indexHtml.Replace("$UserName$", modelUser.UserName);

            if (!string.IsNullOrEmpty(strOtherID))
            {
                indexHtml.Replace("我的相册", modelUser.UserName+"的相册");
            }
            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            //BLL.User_LetterInbox bllInbox = new Jy123.BLL.User_LetterInbox();
            //bllInbox.GetList("FUser = '" + userID + "'");
            indexHtml = indexHtml.Replace("$NewMsg$", newLetterCount.ToString());

            //用户拥有的相册数量
            countAlbum = new BLL.User.Album().AlbumCount(userID.ToString());

            string strAlbumList = string.Empty;
            if (!string.IsNullOrEmpty(strOtherID))
            {
                strAlbumList = AlbumListOther(userID);
            }
            else
            {
                strAlbumList = AlbumList(userID);
            }

            indexHtml.Replace("$AlbumListli$",strAlbumList );
            indexHtml.Replace("$CountAlbum$", countAlbum.ToString());
            Response.Write(indexHtml.ToString());
        }
        //我的相册列表
        private string AlbumList(int userID)
        {
            
            //相册列表
            DataTable dtAlbumList = new DataTable();
            dtAlbumList = new BLL.User.Album().AlbumList_My(currentpage, pagesize, "FUser = '" + userID + "'", "ID", out count).Tables[0];
            StringBuilder sbAlbumListLi = new StringBuilder();
            for (int i = 0; i < dtAlbumList.Rows.Count; i++)
            {
                string strHtml = Cache["user_albumlistli"].ToString();
                strHtml = strHtml.Replace("$Title$", dtAlbumList.Rows[i]["Title"].ToString());
                strHtml = strHtml.Replace("$FilePath$", new BLL.User.Album().AlbumCover((int)dtAlbumList.Rows[i]["ID"]))
                .Replace("$UserName$", dtAlbumList.Rows[i]["UserName"].ToString())
                .Replace("$ImagesCount$", new BLL.User.Album().PhotoCount((int)dtAlbumList.Rows[i]["ID"]).ToString())
                .Replace("$LastUploadTime$", dtAlbumList.Rows[i]["LastUploadTime"].ToString())
                .Replace("$CreateTime$", dtAlbumList.Rows[i]["CreateTime"].ToString())
                .Replace("$AlbumID$", dtAlbumList.Rows[i]["ID"].ToString());

                //相册中的照片列表
                StringBuilder sbPhoto = new StringBuilder();
                //DataTable dtPhoto = new BLL.User_Photo().GetList("FAlbum='" + i + "'").Tables[0];
                DataTable dtPhoto = new BLL.User.Album().PhotoList((int)dtAlbumList.Rows[i]["ID"], "ID desc", 1, 4).Tables[0];
                for (int j = 0; j < dtPhoto.Rows.Count; j++)
                {
                    string photoHtml = Cache["user_albumlistliImglist"].ToString();

                    photoHtml = photoHtml.Replace("$Description$", dtPhoto.Rows[j]["Description"].ToString())
                        .Replace("$PhotoID$", dtPhoto.Rows[j]["ID"].ToString())
                        .Replace("$AlbumID$", dtAlbumList.Rows[i]["ID"].ToString());

                    //判断图片是否需要处理,大图被缩略，小图不动 
                    bool takePic = new AppCode.CreateDir().EqualWH(dtPhoto.Rows[j]["FilePath"].ToString(), 100, 100);
                    if (takePic)
                    {
                        photoHtml = photoHtml.Replace("$FilePath$", new AppCode.CreateDir().ReDraw(100, 0, (dtPhoto.Rows[j]["FilePath"].ToString())));
                    }
                    else
                    {
                        photoHtml = photoHtml.Replace("$FilePath$", dtPhoto.Rows[j]["FilePath"].ToString());
                    }
                    //photoHtml = photoHtml.Replace("$FilePath$", new AppCode.CreateDir().ReDraw(100, 0, (dtPhoto.Rows[j]["FilePath"].ToString())));
                    sbPhoto.Append(photoHtml);//加入一张照片
                }

                //strHtml = strHtml.Replace("$AlbumListliImglist$", dtAlbumList.Rows[i][Cache["user_albumlistliImglist"].ToString()].ToString());
                strHtml = strHtml.Replace("$AlbumListliImglist$", sbPhoto.ToString());

                sbAlbumListLi.Append(strHtml);//加入1行Li
                //sbAlbumListLi.Append(sbPhoto.ToString());                    
            }
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>";
            string strHref = "MyAlbum.aspx?page=$num$";
            MoonPage mp = new MoonPage(false, true);
            mp.Count = Convert.ToInt32(count[1]);
            sbAlbumListLi.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > count[0]) ? count[0] : currentpage * pagesize, count[0]);
            
            return sbAlbumListLi.ToString();
        }
        //别人的相册列表
        private string AlbumListOther(int userID)
        {
            //相册列表
            string role = Request.QueryString["r"];
            DataTable dtAlbumList = new DataTable();
            dtAlbumList = new BLL.User.Album().AlbumList_My(currentpage, pagesize, "FUser = '" + userID + "'", "ID", out count).Tables[0];
            StringBuilder sbAlbumListLi = new StringBuilder();
            for (int i = 0; i < dtAlbumList.Rows.Count; i++)
            {
                //string dtAlbumCover = new BLL.User.Album().AlbumCover((int)dtAlbumList.Rows[i]["ID"]);
                string strHtml = Cache["user_albumfriendlistli"].ToString();
                strHtml = strHtml.Replace("$Title$", dtAlbumList.Rows[i]["Title"].ToString());
                strHtml = strHtml.Replace("$FilePath$", new BLL.User.Album().AlbumCover((int)dtAlbumList.Rows[i]["ID"]));
                strHtml = strHtml.Replace("$UserName$", dtAlbumList.Rows[i]["UserName"].ToString())
                .Replace("$ImagesCount$", new BLL.User.Album().PhotoCount((int)dtAlbumList.Rows[i]["ID"]).ToString())//.Replace("$ImagesCount$", dtAlbumList.Rows[i]["ImagesCount"].ToString())
                .Replace("$LastUploadTime$", dtAlbumList.Rows[i]["LastUploadTime"].ToString())
                .Replace("$CreateTime$", dtAlbumList.Rows[i]["CreateTime"].ToString())
                .Replace("$AlbumID$", dtAlbumList.Rows[i]["ID"].ToString());

                //相册中的照片列表
                StringBuilder sbPhoto = new StringBuilder();
                //DataTable dtPhoto = new BLL.User_Photo().GetList("FAlbum='" + i + "'").Tables[0];
                DataTable dtPhoto = new BLL.User.Album().PhotoList((int)dtAlbumList.Rows[i]["ID"], "ID desc", currentpage, 4).Tables[0];
                for (int j = 0; j < dtPhoto.Rows.Count; j++)
                {
                    string photoHtml = Cache["user_albumlistliImglist_Friend"].ToString();

                    photoHtml = photoHtml.Replace("$Description$", dtPhoto.Rows[j]["Description"].ToString())
                        .Replace("$PhotoID$", dtPhoto.Rows[j]["ID"].ToString())
                        .Replace("$AlbumID$", dtAlbumList.Rows[i]["ID"].ToString());

                    //判断图片是否需要处理,大图被缩略，小图不动 
                    bool takePic = new AppCode.CreateDir().EqualWH(dtPhoto.Rows[j]["FilePath"].ToString(), 100, 100);
                    if (!string.IsNullOrEmpty(dtPhoto.Rows[j]["FilePath"].ToString()))
                    {
                        if (takePic)
                        {
                            photoHtml = photoHtml.Replace("$FilePath$", new AppCode.CreateDir().ReDraw(100, 0, (dtPhoto.Rows[j]["FilePath"].ToString())));
                        }
                        else
                        {
                            photoHtml = photoHtml.Replace("$FilePath$", dtPhoto.Rows[j]["FilePath"].ToString());
                        }

                    }

                    //photoHtml = photoHtml.Replace("$FilePath$",new AppCode.CreateDir().ReDraw(90,103,(dtPhoto.Rows[j]["FilePath"].ToString())));
                    sbPhoto.Append(photoHtml);//加入一张照片
                }

                //strHtml = strHtml.Replace("$AlbumListliImglist$", dtAlbumList.Rows[i][Cache["user_albumlistliImglist"].ToString()].ToString());
                strHtml = strHtml.Replace("$AlbumListliImglist$", sbPhoto.ToString());

                sbAlbumListLi.Append(strHtml);//加入1行Li
                //sbAlbumListLi.Append(sbPhoto.ToString());                    
            }
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>";
            string strHref = "MyAlbum.aspx?page=$num$&userID="+userID;
            MoonPage mp = new MoonPage(false, true);
            mp.Count = Convert.ToInt32(count[1]);
            sbAlbumListLi.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > count[0]) ? count[0] : currentpage * pagesize, count[0]);
            
            return sbAlbumListLi.ToString();
        }
    }
}
